﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CodeRobot.Utility
{
    /// <summary>
    /// 字符处理
    /// </summary>
    public class StringHelper
    {
        /// <summary>
        /// 根据MySQL数据类型转换成C#中的数据类型
        /// </summary>
        /// <param name="strDBType">数据类型</param>
        /// <returns></returns>
        public static string GetCSharpDBType(string strDBType)
        {
            switch (strDBType)
            {
                case "tinyint":
                case "smallint":
                case "mediumint":
                case "int":
                case "bigint":
                case "integer":
                    return "int";
                case "double":
                    return "double";
                case "float":
                    return "float";
                case "decimal":
                    return "double";
                case "numeric":
                case "real":
                    return "decimal";
                case "bit":
                    return "bool";
                case "date":
                case "time":
                case "year":
                case "datetime":
                case "timestamp":
                    return "DateTime";
                case "tinyblob":
                case "blob":
                case "mediumblob":
                case "longblog":
                case "binary":
                case "varbinary":
                    return "byte[]";
                case "char":
                case "varchar":
                case "tinytext":
                case "text":
                case "mediumtext":
                case "longtext":
                    return "string";
                case "point":
                case "linestring":
                case "polygon":
                case "geometry":
                case "multipoint":
                case "multilinestring":
                case "multipolygon":
                case "geometrycollection":
                case "enum":
                case "set":
                default:
                    return strDBType;
            }
        }


        /// <summary>
        /// 生成首字母大写表名
        /// </summary>
        /// <param name="strTableName">表名</param>
        /// <returns></returns>
        public static string GetTableNameUpper(string strTableName)
        {
            string strReturnValue = "";

            try
            {
                if (strTableName.Contains("_"))
                {
                    string[] splitTableName = strTableName.Split(new char[] { '_' });
                    string strWordOne = splitTableName[0].ToString();
                    string strWordTwo = splitTableName[1].ToString();

                    string strNewWordOne = strWordOne.Substring(0, 1);
                    strWordOne = strNewWordOne.ToUpper() + strWordOne.Substring(1);

                    string strNewstrWordTwo = strWordTwo.Substring(0, 1);
                    strWordTwo = strNewstrWordTwo.ToUpper() + strWordTwo.Substring(1);

                    strReturnValue = strWordOne + strWordTwo;

                }
                else
                {
                    string strWords = strTableName.Substring(0, 1);
                    strWords = strWords.ToUpper() + strTableName.Substring(1);
                    strReturnValue = strWords;
                }
            }
            catch (Exception ex)
            {
               LogHelper.Error(typeof(StringHelper), ex, "生成首字母大写表名", "GetTableNameUpper");
            }

            return strReturnValue;
        }

        /// <summary>
        /// 获取类名，过滤“_”前面的字符
        /// </summary>
        /// <param name="strTableName">表名</param>
        /// <returns></returns>
        public static string GetClassName(string strTableName)
        {
            string strReturnValue = "";

            try
            {
                if (strTableName.Contains("_"))
                {
                    string[] splitNames = strTableName.Split(new char[] { '_' });
                    if (splitNames.Length == 2)
                    {
                        strReturnValue = splitNames[1];
                    }
                    if (splitNames.Length == 3)
                    {
                        string strBehindStr = splitNames[2];
                        strBehindStr = GetTableNameUpper(strBehindStr);
                        strReturnValue = splitNames[1] + strBehindStr;
                    }

                }
            }
            catch (Exception ex)
            {
                LogHelper.Error(typeof(StringHelper), ex, "获取类名", "GetClassName");
            }

            return strReturnValue;
        }

        /// <summary>
        /// 获取首字母小写表名
        /// </summary>
        /// <param name="strTableName">表名</param>
        /// <returns></returns>
        public static string GetTableNameFirtWordLower(string strTableName)
        {
            string strReturnValue = "";

            try
            {
                if (strTableName.Contains("_"))
                {
                    string[] splitNames = strTableName.Split(new char[] { '_' });
                    string strBehind = splitNames[1].ToString();

                    string strNewWordOne = strBehind.Substring(0, 1);
                    strNewWordOne = strNewWordOne.ToLower();
                    strReturnValue = strNewWordOne + strBehind.Substring(1, strBehind.Length - 1);
                }
                else
                {
                    string strNewWordOne = strTableName.Substring(0, 1);
                    strNewWordOne = strNewWordOne.ToLower();
                    strReturnValue = strNewWordOne + strTableName.Substring(1, strTableName.Length - 1);
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error(typeof(StringHelper), ex, "获取首字母小写表名", "GetTableNameFirtWordLower");
            }

            return strReturnValue;
        }

        /// <summary>
        /// 处理表中的列名注释
        /// </summary>
        /// <param name="strColumnComment">列名注释</param>
        /// <returns></returns>
        public static string GetColumnComment(string strColumnComment)
        {
            string strReturnValue = "";

            try
            {
                strColumnComment = strColumnComment.Replace("{下拉}", "");
                strColumnComment = strColumnComment.Replace("{勾选}", "");
                strColumnComment = strColumnComment.Replace("{单选}", "");
                strColumnComment = strColumnComment.Replace("{复选}", "");
                strColumnComment = strColumnComment.Replace("{图片}", "");
                strColumnComment = strColumnComment.Replace("：0=否，1=是", "");

                if (strColumnComment.Contains("]"))
                {
                    string[] splitStr = strColumnComment.Split(new char[] { ']' });
                    strReturnValue = splitStr[1];
                }
                else
                {
                    strReturnValue = strColumnComment;
                }

            }
            catch(Exception ex)
            {
                LogHelper.Error(typeof(StringHelper), ex, "处理表中的列名注释", "GetColumnComment");
            }

            return strReturnValue;
        }
    }
}
